//menu collapse
$('.navMenu li a').on('click', function() {
    var parent = $(this).parent().parent(); //获取当前页签的父级的父级
    var labeul = $(this).parent("li").find(">ul")
    if ($(this).parent().hasClass('open') == false) {
        //展开未展开
        parent.find('ul').slideUp(300);
        parent.find("li").removeClass("open")
        parent.find('li a').removeClass("active").find(".arrow").removeClass("open")
        $(this).parent("li").addClass("open").find(labeul).slideDown(300);
        $(this).addClass("active").find(".arrow").addClass("open")
    } else {
        $(this).parent("li").removeClass("open").find(labeul).slideUp(300);
        if ($(this).parent().find("ul").length > 0) {
            $(this).removeClass("active").find(".arrow").removeClass("open")
        } else {
            $(this).addClass("active")
        }
    }
});
//menu collapse over

function myFormFunc() {
    $("#begin_date").datetimepicker({
        format: 'yyyy-mm-dd', //格式
        autoclose: true, // 选择日期后关闭
        startview: 3, //打开默认显示：0小时1天2月3年
        // 选择器所能够提供的最精确的时间选择视图
        // 0表示分钟(默认),1表示小时,2表示天,3表示月,4表示年
        minView: 2,
        language: 'zh-CN', //显示语言为中文
        todayBtn: true,
        todayHighlight: true,
        pickerPosition: "bottom-left"
    });
    $("#end_date").datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        startview: 3,
        minView: 2,
        language: 'zh-CN',
        todayBtn: true,
        todayHighlight: true,
        pickerPosition: "bottom-left"
    });
    $("#sensor").selectpicker();
    var preDate = new Date(new Date() - 24 * 60 * 60 * 1000);
    var curDate = new Date();
    var strPreDateMonth = preDate.getMonth() + 1;
    var strCurDateMonth = curDate.getMonth() + 1;
    if (strPreDateMonth >= 1 && strPreDateMonth <= 9) {
        strPreDateMonth = '0' + strPreDateMonth;
    }
    if (strCurDateMonth >= 1 && strCurDateMonth <= 9) {
        strCurDateMonth = '0' + strCurDateMonth;
    }
    var strPreDateDate = preDate.getDate();
    var strCurDateDate = curDate.getDate();
    if (strPreDateDate >= 1 && strPreDateDate <= 9) {
        strPreDateDate = '0' + strPreDateDate;
    }
    if (strCurDateDate >= 1 && strCurDateDate <= 9) {
        strCurDateDate = '0' + strCurDateDate;
    }
    var strPreDate = preDate.getFullYear() + '-' + strPreDateMonth + '-' + strPreDateDate;
    var strCurDate = curDate.getFullYear() + '-' + strCurDateMonth + '-' + strCurDateDate;
    $("#begin_date").val(strPreDate);
    $("#end_date").val(strCurDate);
}
//read hash while loading page
window.onload = function() {
        var hash = window.location.hash;
        if (hash == null) return;
        $(hash).click();


        var myid = hash;

        $(myid).find("a").addClass("active")
        $(myid).parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")
        $(myid).parent("ul").parent("li").parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")
        $(myid).parent("ul").parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")

        $(myid).parent("ul").parent("li").parent("ul").parent("li").parent("ul").slideDown()
        $(myid).parent("ul").parent("li").parent("ul").slideDown()
        $(myid).parent("ul").slideDown()



    }
    //set eventlistener of hashchange, jump and load pagewhen forward or backward
window.onhashchange = function() {
    var hash = window.location.hash;
    if (hash == null) return;
    $(hash).click();

    var myid = hash;

    $(myid).find("a").addClass("active")
    $(myid).parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")
    $(myid).parent("ul").parent("li").parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")
    $(myid).parent("ul").parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("open").children("a").addClass("active").find(".arrow").addClass("open")

    $(myid).parent("ul").parent("li").parent("ul").parent("li").parent("ul").slideDown()
    $(myid).parent("ul").parent("li").parent("ul").slideDown()
    $(myid).parent("ul").slideDown()
}

//dynamic loading oldpages on the right and active datepicker.
$("#classrooms").click(function() {
    $("#mainpage").load("classrooms.html", function() {
        window.location.hash = "classrooms";
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
$("#myhistory_wating").click(function() {
    $("#mainpage").load("myhistory_waiting.html", function() {
        window.location.hash = "myhistory_waiting";
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
$("#myhistory_passed").click(function() {
    $("#mainpage").load("myhistory_passed.html", function() {
        window.location.hash = "myhistory_passed";
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
$("#general_wating").click(function() {
    $("#mainpage").load("general_wating.html", function() {
        window.location.hash = "general_wating";
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
$("#advanced_wating").click(function() {
    $("#mainpage").load("advanced_wating.html", function() {
        window.location.hash = "advanced_wating";
        $("#time").selectpicker();
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
$("#advanced_passed").click(function() {
    $("#mainpage").load("advanced_passed.html", function() {
        window.location.hash = "advanced_passed";
        $("#time").selectpicker();
        myFormFunc();
        equals();
        getSensorList();
        scrollTo(0, 0);
    });
});
function equals() { //使左查询条件框与右说明框高度一致
    var left_var = $("#left");
    var right_var = $("#right");
    if (left_var.outerHeight() > right_var.outerHeight()) {
        right_var.outerHeight(left_var.outerHeight());
    } else {
        left_var.outerHeight(right_var.outerHeight());
    }
}

$logout=$("#logout");
//注销按钮
$logout.click(function () {
    $.get('logout.php');
    document.cookie='account=';
    window.location.href="login.html";
    });

//右上角显示用户名
var arrCookie=document.cookie.split(";");
var account;
for(var i=0;i<arrCookie.length;i++){
    if(/account=/.test(arrCookie[i])){
        account=arrCookie[i].replace("account=","");
    }
}

$logout.popover({
    title:"当前用户:"+account,
    placement:'bottom',
    trigger:'hover',
    template:'<div class="popover"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3></div></div></div>'
});